package com.hfzy.ihk.web.callhistoryold.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hfzy.ihk.common.core.elasticsearch.service.EsBaseServiceImpl;
import com.hfzy.ihk.common.util.string.StringTools;
import com.hfzy.ihk.web.callhistoryold.dao.CallhistoryPlayRecordRepository;
import com.hfzy.ihk.web.callhistoryold.entity.CallhistoryPlayRecord;
import com.hfzy.ihk.web.callhistoryold.rmi.client.CallHistoryLcRmiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class CallhistoryPlayRecordBiz extends EsBaseServiceImpl<CallhistoryPlayRecord> {

    @Autowired
    CallhistoryPlayRecordRepository callhistoryPlayRecordRepository;

    @Autowired
    CallHistoryLcRmiService callHistoryLcRmiService;

    @Override
    protected ElasticsearchCrudRepository getReposity() {
        return callhistoryPlayRecordRepository;
    }


    public void buildDatasFromHBase(int limit) {


        JSONArray jArr = null;
        String startId = null;
        do {
            List<CallhistoryPlayRecord> recordList = new ArrayList<CallhistoryPlayRecord>();
            String rsStr = callHistoryLcRmiService.getAllCallhistoryPlayRecords(limit, startId);
            if(StringTools.isNotempty(rsStr)) {
                jArr = JSONArray.parseArray(rsStr);
                for (int j = 0; j < jArr.size(); j++) {
                    JSONObject jObj = jArr.getJSONObject(j);
                    if (jObj != null) {
                        CallhistoryPlayRecord record = JSONObject.parseObject(jObj.toString(), CallhistoryPlayRecord.class);
                        startId = record.getId();
                        recordList.add(record);
                    }
                }
            }
            if (recordList.size() > 0) {
                insertBatch(recordList, limit);
            }
        } while (jArr.size() >= limit);
    }

    public void addCallhistoryPlayRecord(String json){

        if(StringTools.isNotempty(json)){
            CallhistoryPlayRecord record = JSONObject.parseObject(json,CallhistoryPlayRecord.class);
            if(record != null){
                insert(record);
            }
        }

    }
}
